'use strict';

app.controller('countCtrl', ['$scope', '$http', '$stateParams', '$rootScope', '$filter', '$location', '$timeout','lodash', function ($scope, $http, $stateParams, $rootScope, $filter, $location, $timeout,lodash) {
    $rootScope.initBreadcrumb($scope.breadcrumb);
    $scope.breadcrumb.push("需求管理",'SLA统计');
    $rootScope.initHeaderElements($scope.settings);
    var backUrl = {
        show: true,
        label: "返回",
        func: function () {
            $rootScope.backHistory();
        }
    }
    $scope.settings.btnEle.push(backUrl);


        

    $scope.info = {
        userList: [],         //用户列表
        departmentList: [],   //部门列表
        productList: [],      //产品列表
        creatorList: [],      //提出人列表
        toAccountList: [],    //受理人列表
        pieData: [],
        viewDay: [],
        splineTimely: [],
        splineNotTimely: [],
        isPDM: $rootScope.isMatrixCusRole('pdm'),
    };

    getAllUser();
    getDepartmengList();
    getDProductList();
    setPie();
    setSplineAccept();
    setSplineComplete();

    //获取全部用户
    function getAllUser() {
        var url = beetle_auth_url + "/user/?token_id=" + getTokenId();
        $http.get(url).success(function(result) {
            if(result.success) {
                angular.forEach(result.data, function(res) {
                    res.crtname = res.realname;
                });
                $scope.info.userList = result.data;
                $scope.info.creatorList = angular.copy($scope.info.userList);
                $scope.info.toAccountList = angular.copy($scope.info.userList);
            } else {
                $rootScope.toasterErrorInfo("获取用户失败！");
            }
        });
    };

    //获取全部部门
    function getDepartmengList() {
        var needSource = $rootScope.getSysConfigList('NEED_SOURCE');
        angular.forEach(needSource, function(source) {
            source.crtname = source.value;
        });
        $scope.info.departmentList = needSource;
        // var url = "../data/count.json";
        // $http.get(url).success(function(result) {
        //     if(result.code == "0") {
        //         $scope.info.departmentList = result.data.departmengList;
        //     } else {
        //         $rootScope.toasterErrorInfo("获取部门数据失败！");
        //     }
        // });
    };

    //获取全部产品组
    function getDProductList() {
        var url = "../data/count.json";
        $http.get(url).success(function(result) {
            if(result.code == "0") {
                $scope.info.productList = result.data.productList;
            } else {
                $rootScope.toasterErrorInfo("获取产品组数据失败！");
            }
        });
    };

    // 设置pie图
    function setPie() {
        $scope.info.isShowPie = false;
        var url = matrix_product_url + "/need/getStatusCount?statusList=" + JSON.stringify(["NO_ACCEPT", "ACCEPT", "CONFIRM", "DEVING", "ONLINE", "LATED"]);
        $http.get(url).success(function(result) {
            if (result.code == "0") {
                var crtTime = new Date().getTime()/1000;
                $scope.info.startDate = timestampToTime((crtTime-30*86400)*1000, true);
                $scope.info.isShowPie = true;
                $scope.info.pieData = [
                    { "label": "新建/未受理（" + result.data.NO_ACCEPT + "个）", "data": result.data.NO_ACCEPT },
                    { "label": "需求受理（" + result.data.ACCEPT + "个）", "data": result.data.ACCEPT },
                    { "label": "需求确认（" + result.data.CONFIRM + "个）", "data": result.data.CONFIRM },
                    { "label": "立项研发（" + result.data.DEVING + "个）", "data": result.data.DEVING },
                    { "label": "完成上线（" + result.data.ONLINE + "个）", "data": result.data.ONLINE },
                    { "label": "延期中（" + result.data.LATED + "个）", "data": result.data.LATED },
                ];
            } else {
                $rootScope.toasterErrorInfo("获取需求状态数据错误！");
            }
        });
    }
   
    // 设置spline图--ACCEPT
    function setSplineAccept() {
        $scope.info.isShowTimely = false;
        var url = matrix_product_url + "/need/getAcceptanceProbability?type=ACCEPT";
        $http.get(url).success(function(result) {
            if (result.code == "0") {
                var num = 1;
                var viewTimelyDay = [];
                var splineTimely = [];
                $scope.info.viewTimelyDay = [];
                $scope.info.splineTimely = [];
                angular.forEach(result.data, function(data, value) {
                    $scope.info.isShowTimely = true;
                    viewTimelyDay.push([num++, value]);
                    splineTimely.push([num++, data]);
                });
                for(var i = 1; i <= viewTimelyDay.length; i++) {
                    $scope.info.viewTimelyDay.push([i,viewTimelyDay[i-1][1]]);
                }
                for(var i = 1; i <=splineTimely.length; i++) {
                    $scope.info.splineTimely.push([i, splineTimely[i-1][1]]);
                }
            } else {
                $rootScope.toasterErrorInfo("获取需求处理数据错误！");
            }
        });
    }

    // 设置spline图--COMPLETE
    function setSplineComplete() {
        $scope.info.isShowNotTimely = false;
        var url = matrix_product_url + "/need/getAcceptanceProbability?type=COMPLETE";
        $http.get(url).success(function(result) {
            if (result.code == "0") {
                var viewNotTimelyDay = [];
                var splineNotTimely = [];
                $scope.info.viewNotTimelyDay = [];
                $scope.info.splineNotTimely = [];
                angular.forEach(result.data, function(data, value) {
                    $scope.info.isShowNotTimely = true;
                    viewNotTimelyDay.push([0, value]);
                    splineNotTimely.push([0, data]);
                });
                for(var i = 1; i <= viewNotTimelyDay.length; i++) {
                    $scope.info.viewNotTimelyDay.push([i,viewNotTimelyDay[i-1][1]]);
                }
                for(var i = 1; i <=splineNotTimely.length; i++) {
                    $scope.info.splineNotTimely.push([i, splineNotTimely[i-1][1]]);
                }
            } else {
                $rootScope.toasterErrorInfo("获取需求处理数据错误！");
            }
        });
    }

    // 执行查询
    $scope.getPieAndSpline = function() {
        var startDate = Date.parse($scope.info.startDate),
            endDate = Date.parse($scope.info.endDate),
            proposerAccount = $scope.info.sltCreatorItem ? $scope.info.sltCreatorItem.account : "",
            pdmAccount = $scope.info.sltAccountItem ? $scope.info.sltAccountItem.account : "",
            source = $scope.info.sltDepartItem ? $scope.info.sltDepartItem : "";
        getPieData(startDate, endDate, proposerAccount, pdmAccount, (source.key || ""));
        getSplineAccept(startDate, endDate, proposerAccount, pdmAccount, (source.key || ""));
        getSplineComplete(startDate, endDate, proposerAccount, pdmAccount, (source.key || ""));
    };

    // 根据参数查询pie
    function getPieData(startDate, endDate, proposerAccount, pdmAccount, source) {
        $scope.info.isShowPie = false;
        var statusList = JSON.stringify(["NO_ACCEPT", "ACCEPT", "CONFIRM", "DEVING", "ONLINE", "LATED"]);
        var url = matrix_product_url +"/need/getStatusCount?statusList="+ statusList +"&starDate="+ startDate +"&endDate="+ endDate;
        if(proposerAccount) {
            url = url + "&proposerAccount="+ proposerAccount;
        }
        if(pdmAccount) {
            url = url + "&pdmAccount="+ pdmAccount;
        }
        if(source) {
            url = url + "&source="+ source;
        }
        $http.get(url).success(function(result) {
            if(result.code == "0") {
                $scope.info.isShowPie = true;
                $scope.info.pieData = [
                    { "label": "新建/未受理（" + result.data.NO_ACCEPT + "个）", "data": result.data.NO_ACCEPT },
                    { "label": "需求受理（" + result.data.ACCEPT + "个）", "data": result.data.ACCEPT },
                    { "label": "需求确认（" + result.data.CONFIRM + "个）", "data": result.data.CONFIRM },
                    { "label": "立项研发（" + result.data.DEVING + "个）", "data": result.data.DEVING },
                    { "label": "完成上线（" + result.data.ONLINE + "个）", "data": result.data.ONLINE },
                    { "label": "延期中（" + result.data.LATED + "个）", "data": result.data.LATED },
                ];
            } else {
                $rootScope.toasterErrorInfo("查询状态分布数据错误，请重试！");
            }
        });
    }

    // 根据参数查询spline--ACCEPT
    function getSplineAccept(startDate, endDate, proposerAccount, pdmAccount, source) {
        $scope.info.isShowTimely = false;
        var url = matrix_product_url + "/need/getAcceptanceProbability?type=ACCEPT&starDate="+ startDate +"&endDate="+ endDate;
        if(proposerAccount) {
            url = url + "&proposerAccount="+ proposerAccount;
        }
        if(pdmAccount) {
            url = url + "&pdmAccount="+ pdmAccount;
        }
        if(source) {
            url = url + "&source="+ source;
        }
        $http.get(url).success(function(result) {
            if (result.code == "0") {
                var num = 1;
                var viewTimelyDay = [];
                var splineTimely = [];
                $scope.info.viewTimelyDay = [];
                $scope.info.splineTimely = [];
                angular.forEach(result.data, function(data, value) {
                    $scope.info.isShowTimely = true;
                    viewTimelyDay.push([num++, value]);
                    splineTimely.push([num++, data]);
                });
                for(var i = 1; i <= viewTimelyDay.length; i++) {
                    $scope.info.viewTimelyDay.push([i,viewTimelyDay[i-1][1]]);
                }
                for(var i = 1; i <=splineTimely.length; i++) {
                    $scope.info.splineTimely.push([i, splineTimely[i-1][1]]);
                }
            } else {
                $rootScope.toasterErrorInfo("获取需求处理数据错误！");
            }
        });
    };

    // 根据参数查询spline--COMPLETE
    function getSplineComplete(startDate, endDate, proposerAccount, pdmAccount, source) {
        $scope.info.isShowNotTimely = false;
        var url = matrix_product_url + "/need/getAcceptanceProbability?type=COMPLETE&starDate="+ startDate +"&endDate="+ endDate;
        if(proposerAccount) {
            url = url + "&proposerAccount="+ proposerAccount;
        }
        if(pdmAccount) {
            url = url + "&pdmAccount="+ pdmAccount;
        }
        if(source) {
            url = url + "&source="+ source;
        }
        $http.get(url).success(function(result) {
            if (result.code == "0") {
                var index = 1;
                var viewNotTimelyDay = [];
                var splineNotTimely = [];
                $scope.info.viewNotTimelyDay = [];
                $scope.info.splineNotTimely = [];
                angular.forEach(result.data, function(data, value) {
                    $scope.info.isShowNotTimely = true;
                    viewNotTimelyDay.push([index++, value]);
                    splineNotTimely.push([index++, data]);
                });
                for(var i = 1; i <= viewNotTimelyDay.length; i++) {
                    $scope.info.viewNotTimelyDay.push([i,viewNotTimelyDay[i-1][1]]);
                }
                for(var i = 1; i <=splineNotTimely.length; i++) {
                    $scope.info.splineNotTimely.push([i, splineNotTimely[i-1][1]]);
                }
            } else {
                $rootScope.toasterErrorInfo("获取需求处理数据错误！");
            }
        });
    };
}]);